/**
 * @param url  //请求地址
 * @param obj  //zTree的id
 * @param nodes  //保存节点的属性
 * @param isSelectMulti //是否可以多选，默认是false
 * @param isLeafNode  //是否只容许选择叶子节点，默认是false
 * @param fn  //回调方法
 *
 * */

function ztreeSelect(url,obj,nodes,isSelectMulti,isLeafNode,fn){
     var treeObj = obj;
     var zNodes =  nodes;
     var selectedMulti = false;
     var leafNode = false;
    if(isSelectMulti !=null && isSelectMulti != undefined){
        selectedMulti = isSelectMulti;
    }
    if(isLeafNode !=null && isLeafNode != undefined){
        leafNode = isLeafNode;
    }
     var setting = {
      view: {
        showLine: false,
        showIcon: false,
        selectedMulti: false,
        dblClickExpand: false,
        addDiyDom: addDiyDom
      },
      data: {
        simpleData: {
          enable: true
        }
      },
      callback: {
        beforeClick: beforeClick,
        onClick: zTreeOnClick
      }
    };
    if(selectedMulti){
        setting.check= {
            enable: true,
            chkboxType: {"Y":"p", "N":"p"}
        }
        setting.view={
            dblClickExpand: false
        }
    }
    
    $.ajax({
    type:"post",
    data:"json",
    url:contextPath+url,
    async:false,
    success:function (result){
      var result2=result;
      var newResult=[];
      for(var i=0;i<result2.length;i++){
        newResult.push({"id":result2[i].id,"pId":result2[i].pid,"name":result2[i].name});
        }
       zNodes=newResult;    
      }
      
    });
    function addDiyDom(treeId, treeNode) {
      var spaceWidth = 10;
      var switchObj = $("#" + treeNode.tId + "_switch"),
      icoObj = $("#" + treeNode.tId + "_ico");
      //console.log(treeNode.tId)     
      switchObj.remove();
      icoObj.before(switchObj);

      if (treeNode.level > 0) {
        var spaceStr = "<span style='display: inline-block;width:" + (spaceWidth * treeNode.level)+ "px'></span>";
        switchObj.before(spaceStr);
      }
      
    }

    function beforeClick(treeId, treeNode) {
      return true;
    }
    
    function zTreeOnClick(event, treeId, treeNode) {
      if(leafNode){
          if(treeNode.isParent){
              dialog({
                  title:'提示',
                  content:'请选中叶子节点！',
                  okValue: '关闭',
                  zindex:1080,
                  ok:function(){}
              }).show();
          }else{
              var parentName=treeNode.getParentNode()!=null&&treeNode.pId!=1?treeNode.getParentNode().name:'';
              treeObj.parent().parent().find('>input').val(parentName+treeNode.name).attr('data_id',treeNode.id).attr('parent',treeNode.pId);
          }
      }else{
          var parentName=treeNode.getParentNode()!=null&&treeNode.pId!=1?treeNode.getParentNode().name:'';
          treeObj.parent().parent().find('>input').val(parentName+treeNode.name).attr('data_id',treeNode.id).attr('parent',treeNode.pId);
      }
      
    };
   
    var thewidth = treeObj.parent().parent().find('>input').width();   
    treeObj.parent().css({width:thewidth+'px'});    
    var $zTreeDemo = treeObj.parent();
    var thetree = $.fn.zTree.init(treeObj, setting, zNodes);

    treeObj.hover(function () {
      if (!treeObj.hasClass("showIcon")) {
        treeObj.addClass("showIcon");
      }
    }, function() {
      treeObj.removeClass("showIcon");
    });

    treeObj.parent().parent().find('>input').on('mouseover',function(){
      var width = $(this).outerWidth();
      var left = $(this).position().left;
      $zTreeDemo.css({width:width+'px',left:left}).show();
    });   

    treeObj.parent().parent().mouseleave(function(event) {
      treeObj.parent().hide();
    });

  }   
    